/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.rcs.folders;


import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author pablo
 */
public abstract class AbstractFolder extends AbstractItem implements VirtualFolder {

  private static final DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy HH:mm", Locale.ENGLISH);
  private static final NumberFormat sizeFormat = NumberFormat.getNumberInstance(Locale.ENGLISH);

  @Override
  public long getContentLength() {
    return -1;
  }

  @Override
  public void sendContent(HttpServletResponse response, HttpServletRequest request, String requestedPath, boolean onlyHeader) throws Exception {
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

    if (!onlyHeader) {
      PrintWriter wr = response.getWriter();
      String uri = request.getRequestURI();
      
      listFolder(wr, requestedPath, uri) ;
    }
  }

  protected void listFolder(PrintWriter wr, String requestedPath, String uri) throws UnsupportedEncodingException {
      boolean isRoot = requestedPath.equals("/");
      String name = isRoot ? "" : getDisplayName();
      String ecnodedName = URLEncoder.encode(name, "UTF-8");
      String parent = uri.substring(0, uri.length() - ecnodedName.length() - 1);
      String parentName = isRoot ? "." : "..";

      wr.format("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">%n"
              + "<html>%n"
              + "<head>%n"
              + "<title>Index of /%s</title>%n"
              + "</head>%n"
              + "<body>%n"
              + "<h1>Index of /%s</h1>%n"
              + "<table>%n"
              + "<tr><td><a href=\"%s\">%s</a></td></tr>%n", name, name, parent, parentName);

      listItems(wr);
      wr.format("</table>%n"
              + "</body>%n"
              + "</html>");
    
  }
  
  protected void listItem(PrintWriter wr, VirtualItem item) throws UnsupportedEncodingException {
    boolean isFolder = item instanceof VirtualFolder;
    String simpleName = item.getDisplayName();
    String urlName = URLEncoder.encode(simpleName, "UTF-8");
    Date date = item.getModifiedDate();

    wr.format("<tr><td><a href=\"%s\">%s</a></td><td align=\"right\">%s  </td><td> %s</td></tr>%n",
            isFolder ? (urlName + "/") : urlName,
            simpleName,
            dateFormat.format(date),
            isFolder ? "-" : item.getContentLength() + "B");
  }

  protected void listItems(PrintWriter wr) throws UnsupportedEncodingException {
    for (VirtualItem item : getChildren())
      listItem(wr, item);
  }

  @Override
  public void sendContentRange(OutputStream output, long start, long length) throws IOException {
    // never called
  }

  @Override
  public String getContentType() {
    return "text/html";
  }

  @Override
  public VirtualItem child(String childName) {
    for (VirtualItem item : getChildren())
      if (item.getName().equals(childName))
        return item;

    return null;
  }

  @Override
  public VirtualItem childFromDisplayName(String name) {
    for (VirtualItem item : getChildren())
      if (item.getDisplayName().equals(name))
        return item;

    return null;
  }
  
  public boolean hasItem(String displayName, List<VirtualItem> items) {
    boolean exist = false;

    for (int i = 0; i < items.size() && !exist; ++i)
      exist = items.get(i).getDisplayName().equals(displayName);

    return exist;
  }

  public boolean hasItem(VirtualItem item, List<VirtualItem> items) {
    if (null == item)
      throw new IllegalArgumentException(String.format("Item can't be null"));

    return hasItem(item.getDisplayName(), items);
  }  
}
